﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports Ionic.Zip

Public Class Report

	Public Shared Function CountDownlineLv1(ByVal offcerId As String) As Integer
		Return DL.Report.CountDownlineLv1(offcerId)
	End Function

	Public Shared Function CountDownlineLv2(ByVal offcerId As String) As Integer
		Return DL.Report.CountDownlineLv2(offcerId)
	End Function

	Public Shared Function CountDownlineLv3(ByVal offcerId As String) As Integer
		Return DL.Report.CountDownlineLv3(offcerId)
	End Function

	Public Shared Function CountDownlineLv4(ByVal offcerId As String) As Integer
		Return DL.Report.CountDownlineLv4(offcerId)
	End Function

	Public Shared Function CountDownlineLv5(ByVal offcerId As String) As Integer
		Return DL.Report.CountDownlineLv5(offcerId)
	End Function

	Public Shared Sub SetYearList(ByVal ddl As DropDownList)
		ddl.DataSource = DL.Report.YearList
		ddl.DataValueField = "AD_YEAR"
		ddl.DataTextField = "BC_YEAR"
		ddl.DataBind()
	End Sub

	Public Shared Sub SetMonthList(ByVal ddl As DropDownList)
		ddl.Items.Add(New ListItem("มกราคม", "01"))
		ddl.Items.Add(New ListItem("กุมภาพันธ์", "02"))
		ddl.Items.Add(New ListItem("มีนาคม", "03"))
		ddl.Items.Add(New ListItem("เมษายน", "04"))
		ddl.Items.Add(New ListItem("พฤษภาคม", "05"))
		ddl.Items.Add(New ListItem("มิถุนายน", "06"))
		ddl.Items.Add(New ListItem("กรกฎาคม", "07"))
		ddl.Items.Add(New ListItem("สิงหาคม", "08"))
		ddl.Items.Add(New ListItem("กันยายน", "09"))
		ddl.Items.Add(New ListItem("ตุลาคม", "10"))
		ddl.Items.Add(New ListItem("พฤศจิกายน", "11"))
		ddl.Items.Add(New ListItem("ธันวาคม", "12"))
	End Sub

    'Modified By: ZaQk, Modified Date: 27/10/2011
	Public Shared Function ReportUT502(ByVal month As String, ByVal year As String, ByVal officerAppId As String) As DataTable
		ReportUT502 = DL.Report.DataUT502(month, year, officerAppId)
        'ReportUT502.WriteXmlSchema("D:\Projects\Real Network\SourceCode\ut502.xsd")
	End Function

    Public Shared Function ReportUT502LRPoint(ByVal month As String, ByVal year As String, ByVal officerAppId As String) As DataTable
        ReportUT502LRPoint = DL.Report.DataUT502LRPoint(month, year, officerAppId)
    End Function

    Public Shared Function ReportUT502PointUsed(ByVal month As String, ByVal year As String, ByVal officerAppId As String) As DataTable
        ReportUT502PointUsed = DL.Report.DataUT502PointUsed(month, year, officerAppId)
    End Function

	Public Shared Function ReportUT503(ByVal month As String, ByVal year As String, ByVal officerId As String) As DataTable
		ReportUT503 = DL.Report.DataUT503(month, year, officerId)
		'ReportUT503.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\ut503.xsd")
	End Function

	Public Shared Function ReportUT504(ByVal month As String, ByVal year As String, ByVal officerId As String) As DataTable
		ReportUT504 = DL.Report.DataUT504(month, year, officerId)
		'ReportUT503.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\ut503.xsd")
	End Function

	Public Shared Function ReportUT505(ByVal month As String, ByVal year As String, ByVal officerId As String) As DataTable
		ReportUT505 = DL.Report.DataUT505(month, year, officerId)
		'ReportUT503.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\ut503.xsd")
	End Function

	Public Shared Function ReportUT506(ByVal month As String, ByVal year As String) As DataTable
		ReportUT506 = DL.Report.DataUT506(month, year)
		'ReportUT506.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\ut506.xsd")
	End Function

    Public Shared Function ReportUT507(ByVal incomeFrom As Nullable(Of Decimal), ByVal incomeTo As Nullable(Of Decimal)) As DataTable
        ReportUT507 = DL.Report.DataUT507(incomeFrom, incomeTo)
        ReportUT507.WriteXmlSchema("D:\Projects\Real Network\SourceCode\ut507.xsd")
    End Function

    Public Shared Function ReportUT507Export(ByVal rptPath As String, ByVal zipPath As String, ByVal outPutSteam As IO.Stream, ByVal incomeFrom As Nullable(Of Decimal), ByVal incomeTo As Nullable(Of Decimal), ByVal oYear As String) As Boolean
        Dim dt As DataTable
        Dim ds As DataSet

        Try
            dt = DL.Report.DataUT507(incomeFrom, incomeTo, oYear)
        Catch ex As Exception
            Throw New Exception("ไม่สามารถดึงข้อมูลจากฐานข้อมูลได้", ex)
        End Try

        Try
            ds = Common.Utility.SplitDataTable(dt, 60000)
        Catch ex As Exception
            Throw New Exception("ไม่สามารถทำการ split ข้อมูลใน DataTable ได้", ex)
        End Try

        Dim i As Integer = 1
        Dim strFileName As String = Nothing
        Dim strListFileName As New List(Of String)

        Try
            For Each dtExport As DataTable In ds.Tables
                Dim cryRpt As New ReportDocument
                cryRpt.Load(rptPath)
                cryRpt.SetDataSource(dtExport)

                strFileName = "TaxInfo_" & Today.ToString("yyyyMMdd")

                Dim CrExportOptions As ExportOptions
                Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
                Dim CrFormatTypeOptions As New ExcelFormatOptions
                CrDiskFileDestinationOptions.DiskFileName = zipPath & strFileName & ".xls"
                CrExportOptions = cryRpt.ExportOptions
                With CrExportOptions
                    .ExportDestinationType = ExportDestinationType.DiskFile
                    .ExportFormatType = ExportFormatType.ExcelRecord
                    .DestinationOptions = CrDiskFileDestinationOptions
                    .FormatOptions = CrFormatTypeOptions
                End With

                strListFileName.Add(CrDiskFileDestinationOptions.DiskFileName)

                i += 1

                cryRpt.Export()
                cryRpt.Dispose()
                cryRpt = Nothing
            Next
        Catch ex As Exception
            Throw New Exception("ไม่สามารถทำการ Export ข้อมูลเป็นไฟล์ Excel ได้", ex)
        End Try

        If strListFileName.Count > 0 Then
            Try
                Using zip As New ZipFile()
                    zip.AddFiles(strListFileName, "")
                    zip.Save(outPutSteam)
                End Using
            Catch ex As Exception
                Throw New Exception("ไม่สามารถทำการ zip ไฟล์ได้", ex)
            End Try

            Try
                For Each strFile As String In strListFileName
                    IO.File.Delete(strFile)
                Next
            Catch ex As Exception
                Throw New Exception("ไม่สามารถลบไฟล์ที่ได้จากการ Export ได้", ex)
            End Try

            Return True
        Else
            'แสดงว่่าไม่มีไฟล์ที่ export ได้
            Return False
        End If

    End Function

    'Modified By: ZaQk, Modified Date: 27/10/2011
	Public Shared Function ReportUT508(ByVal month As String, ByVal year As String, ByVal officerStatus As String, ByVal incomeFrom As String, ByVal incomeTo As String) As DataTable
		ReportUT508 = DL.Report.DataUT508(month, year, officerStatus, incomeFrom, incomeTo)
        'ReportUT508.WriteXmlSchema("D:\Projects\Real Network\SourceCode\ut508.xsd")
	End Function

	Public Shared Function ReportUT509(ByVal month As String, ByVal year As String, ByVal officerStatus As String, ByVal incomeFrom As String, ByVal incomeTo As String) As DataTable
		ReportUT509 = DL.Report.DataUT509(month, year, officerStatus, incomeFrom, incomeTo)
        'ReportUT509.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\ut509.xsd")
    End Function

    'Coding By: ZaQk, Coding Date: 09/10/2011 
    'Modified By: ZaQk, Modified Date: 27/10/2011
    Public Shared Function ReportUT509(ByVal month As String, ByVal year As String, ByVal officerId As String) As DataTable
        ReportUT509 = DL.Report.DataUT509(month, year, officerId)
        'ReportUT509.WriteXmlSchema("D:\Projects\Real Network\SourceCode\ut509.xsd")
    End Function

	Public Shared Function ReportUT510(ByVal month As String, ByVal year As String, ByVal childFrom As String, ByVal childTo As String) As DataTable
		ReportUT510 = DL.Report.DataUT510(month, year, childFrom, childTo)
		'ReportUT510.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\ut510.xsd")
	End Function

	Public Shared Function ReportUT511(ByVal month As String, ByVal year As String, ByVal rvFrom As String, ByVal rvTo As String) As Data.DataTable
		ReportUT511 = DL.Report.DataUT511(month, year, rvFrom, rvTo)
		'ReportUT511.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\ut511.xsd")
	End Function

	Public Shared Function ReportUT512(ByVal month As String, ByVal year As String, ByVal rvFrom As String, ByVal rvTo As String) As Data.DataTable
		ReportUT512 = DL.Report.DataUT512(month, year, rvFrom, rvTo)
        'ReportUT512.WriteXmlSchema("D:\Projects\Real Network\SourceCode\ut512.xsd")
	End Function

	Public Shared Function ReportUT513(ByVal fromDate As String, ByVal toDate As String, ByVal officeAppTel As String, ByVal officeAppId As String) As Data.DataTable
		ReportUT513 = DL.Report.DataUT513(fromDate, toDate, officeAppTel, officeAppId)
		'ReportUT513.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\ut513.xsd")
	End Function

	Public Shared Function ReportUT513_Excel(ByVal fromDate As String, ByVal toDate As String, ByVal officeAppTel As String, ByVal officeAppId As String) As Data.DataTable
		ReportUT513_Excel = DL.Report.DataUT513_Excel(fromDate, toDate, officeAppTel, officeAppId)

		For Each dr As DataRow In ReportUT513_Excel.Rows
			dr("Activity Date") = Convert.ToDateTime(dr("Activity Date").ToString).AddYears(543).ToString("dd/MM/yyyy HH:mm")
		Next

		'ReportUT513.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\ut513.xsd")
	End Function

	Public Shared Function ReportUT514(ByVal fromDate As String, ByVal toDate As String, ByVal fileType As String, ByVal tranStatus As String) As Data.DataTable
		ReportUT514 = DL.Report.DataUT514(fromDate, toDate, fileType.ToUpper, tranStatus.ToUpper)
	End Function

	Public Shared Function ReportUT514_Detail(ByVal fromDate As String, ByVal toDate As String, ByVal fileType As String, ByVal tranStatus As String) As Data.DataTable

		If fromDate Is Nothing Then
			Return Nothing
		Else
			Try
				Dim arrStr() As String = fromDate.Split("/")
				fromDate = arrStr(2) & arrStr(1) & arrStr(0)
			Catch ex As Exception
				Return Nothing
			End Try
		End If

		If toDate Is Nothing Then
			Return Nothing
		Else
			Try
				Dim arrStr() As String = toDate.Split("/")
				toDate = arrStr(2) & arrStr(1) & arrStr(0)
			Catch ex As Exception
				Return Nothing
			End Try
		End If

		Select Case fileType.ToUpper & " " & tranStatus.ToUpper

			Case "SBM FTP FILE SUCCESS"
				Return DL.Report.DataUT514_SBM_FTP_SUCCESS(fromDate, toDate)
			Case "SBM FTP FILE ERROR"
				Return DL.Report.DataUT514_SBM_FTP_ERROR(fromDate, toDate)
			Case "IVR FTP FILE SUCCESS"
				Return DL.Report.DataUT514_IVR_FTP_SUCCESS(fromDate, toDate)
			Case "IVR FTP FILE ERROR"
				Return DL.Report.DataUT514_IVR_FTP_ERROR(fromDate, toDate)
			Case "IVR XML FILE SUCCESS"
				Return DL.Report.DataUT514_IVR_XML_SUCCESS(fromDate, toDate)
			Case "IVR XML FILE ERROR"
				Return DL.Report.DataUT514_IVR_XML_ERROR(fromDate, toDate)
			Case Else
				Return Nothing
		End Select

	End Function

	Public Shared Function ReportUT515(ByVal month As String, ByVal year As String, ByVal officerId As String) As DataTable
		ReportUT515 = DL.Report.DataUT515(month, year, officerId)
		'ReportUT503.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\ut503.xsd")
	End Function

	Public Shared Function ReportUT516(ByVal month As String, ByVal year As String, ByVal officerId As String) As DataTable
		ReportUT516 = DL.Report.DataUT516(month, year, officerId)
		'ReportUT503.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\ut503.xsd")
	End Function

	Public Shared Function ReportUT517(ByVal startDte As String, ByVal endDte As String, ByVal importType As String, ByVal importStatus As String, ByVal appTel As String) As Data.DataTable
		Return DL.Report.DataUT517(startDte, endDte, importType, importStatus, appTel)
	End Function

    'Public Shared Function ReportUT518(ByVal month As String, ByVal year As String, ByVal childFrom As String, ByVal childTo As String) As DataTable
    '	ReportUT518 = DL.Report.DataUT518(month, year, childFrom, childTo)
    '	'ReportUT518.WriteXmlSchema("C:\Documents and Settings\sarayut.se\Desktop\SourceCode\ut518.xsd")
    'End Function
    'Coding By: Yut, Update Coding Date: 22/10/2011
    Public Shared Function ReportUT518(ByVal monthFrom As String, ByVal yearFrom As String, ByVal monthTo As String, ByVal yearTo As String, ByVal childFrom As String, ByVal childTo As String) As DataTable
        ReportUT518 = DL.Report.DataUT518(monthFrom, yearFrom, monthTo, yearTo, childFrom, childTo)
        'ReportUT518.WriteXmlSchema("D:\Projects\Real Network\SourceCode\ut518.xsd")
    End Function

    Public Shared Function ReportUT518New(ByVal monthFrom As String, ByVal yearFrom As String, ByVal monthTo As String, ByVal yearTo As String, ByVal childFrom As String, ByVal childTo As String) As DataTable
        ReportUT518New = DL.Report.DataUT518New(monthFrom, yearFrom, monthTo, yearTo, childFrom, childTo)
        'ReportUT518New.WriteXmlSchema("D:\Projects\Real Network\SourceCode\ut518.xsd")
    End Function

    'Coding By: ZaQk, Coding Date: 09/10/2011
    Public Shared Function ReportUT519(ByVal startDte As String, ByVal dteTo As String, ByVal OfficerAppId As String, ByVal appTel As String, ByVal officerName As String) As DataTable
        ReportUT519 = DL.Report.DataUT519(startDte, dteTo, OfficerAppId, appTel, officerName)
    End Function

    'Coding By: ZaQk, Coding Date: 09/10/2011
    Public Shared Function ReportUT520(ByVal startDte As String, ByVal dteTo As String, ByVal officerId As String) As DataTable
        ReportUT520 = DL.Report.DataUT520(startDte, dteTo, officerId)
    End Function

End Class
